package com.example.weatherview.view

import android.content.Context
import android.graphics.BitmapFactory
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.Paint.Align
import android.util.AttributeSet
import android.view.View
import com.example.weatherview.R
import kotlin.math.roundToInt

class AirView @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : View(context, attrs, defStyleAttr) {



    private val mIndicatorBitmap  = BitmapFactory.decodeResource(resources, R.drawable.ic_sj)


    private val mLinePaint = Paint().apply {
        color = Color.BLACK
        strokeWidth =dp2Px(context,8f).toFloat()
        style = Paint.Style.FILL
    }

    private val mTextPaint = Paint().apply {
        //color=Color.parseColor("#B8FFFFFF")
        color=Color.BLACK
        textAlign = Align.LEFT
        textSize =sp2Px(context,12F).toFloat()
    }


    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        val w = MeasureSpec.getSize(widthMeasureSpec)
        val h = MeasureSpec.getSize(heightMeasureSpec)
        setMeasuredDimension(w,h)
    }


    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
        canvas?.apply {
            drawIndicator()
            drawAirLine()
            drawMaxMin()
        }
    }

    private val mColors = arrayListOf(
        "#50DA60",
        "#E6DE6D",
        "#D27E62",
        "#C44847",
        "#7C5FDF",
        "#491F21"
    )


    private fun Canvas.drawIndicator() {
        val realWidth = width - mIndicatorBitmap.width
        val airMargin = mCurAir*(realWidth / mMax.toFloat())
        drawBitmap(mIndicatorBitmap,airMargin,0F,mLinePaint)
    }


    private fun Canvas.drawMaxMin(){
        val incWidth = mIndicatorBitmap.width/2F
        val dy = mIndicatorBitmap.height + dp2Px(context, 12f+20).toFloat()


        mTextPaint.textAlign = Align.LEFT
        drawText("0",incWidth,dy,mTextPaint)

        val maxX = width- incWidth
        mTextPaint.textAlign = Align.RIGHT
        drawText("$mMax",maxX,dy,mTextPaint)
    }

    private fun Canvas.drawAirLine(){
        val incWidth = mIndicatorBitmap.width/2
        val realWidth = width - incWidth
        val rectWidth = realWidth / 6

        var stopX: Int
        val margin = dp2Px(context, 4f)
        val dy =  mIndicatorBitmap.height+margin*2F

        for (index in 5 downTo  0) {
            mLinePaint.color = getColor(index)
            stopX = (index+1)*rectWidth
            drawLine(incWidth.toFloat(), dy,stopX.toFloat(),dy,mLinePaint)
            invalidate()
        }
    }


    private fun getColor(index: Int):Int{
        val color = Color.BLACK
        if (index < mColors.size){
            val s = mColors[index]
            return Color.parseColor(s)
        }
       return color
    }



    private fun dp2Px(context: Context, dp: Float): Int {
        val density = context.resources.displayMetrics.density
        return (dp * density).roundToInt()
    }

    private fun sp2Px(context: Context, sp: Float): Int {
        val scaledDensity = context.resources.displayMetrics.scaledDensity
        return Math.round(sp * scaledDensity)
    }

    private val mMax = 300


    private var mCurAir = 250



    fun setAirValue(value:Int){
        mCurAir = value
        invalidate()
    }

}